package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {

    @Autowired
    private CustomerTransferMapper customerTransferMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageInfo<CustomerTransfer> selectByQo(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<CustomerTransfer> list = customerTransferMapper.selectAll(qo);
        PageInfo<CustomerTransfer> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void saveOrUpdate(CustomerTransfer customerTransfer) {
        if (customerTransfer.getId() == null) {
            //获取当前操作人信息 设置当前录入时间
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            customerTransfer.setOperator(employee);
            customerTransfer.setOperateTime(new Date());
            customerTransferMapper.insert(customerTransfer);

            //更改客户的销售人员信息
            customerMapper.updateSellerIdByCustomerId(customerTransfer.getNewSeller().getId(),customerTransfer.getCustomer().getId());

        }

    }
}
